<template>
  <view>
    <my-search @click="gotoSearch"></my-search>
    <!-- 分类主页面 -->
    <view class="cate-box">
      <!-- 左侧滚动栏 -->
      <scroll-view scroll-y="true" class="scroll-left" :style="{height:wh+'px'}">
        <block v-for="(item1,i1) in cateList" :key="i1">
          <view :class="['left-scroll-item',active===i1?'active':'']" @click="activeChanged(i1,item1)">
            {{item1.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧滚动区域 -->
      <scroll-view scroll-y="true" class="scroll-right" :style="{height:wh+'px'}" :scroll-top="scroll">
        <!-- 二级盒子 -->
        <view class="level2-box" v-for="(item2,i2) in cateLevel2" :key="i2">
          <view class="level2-title">/ {{item2.cat_name}} /</view>
          <!-- 三级盒子 -->
          <view class="level3-box">
            <view class="level3-item" v-for="(item3,i3) in cateLevel3" :key="i3" @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon" class="levle3-img"></image>
              <text class="level3-title">{{item3.cat_name}}</text>
            </view>
          </view>

        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //页面可视区域
        wh: 0,
        //分类数据
        cateList: [],
        //选中状态的索引
        active: 0,
        //分类的二级数据
        cateLevel2: [],
        //分类的三级数据
        cateLevel3: [],
        //滚动条位置
        scroll: 0
      };
    },
    onLoad() {
      const height = uni.getSystemInfoSync()
      this.wh = height.windowHeight - 50
      this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        this.cateLevel2 = res.message[0].children
        this.cateLevel2.forEach(item => {
          this.cateLevel3 = item.children
        })
        console.log(this.cateLevel3, '=========');
      },
      activeChanged(i, item) {
        this.active = i

        this.cateLevel2 = item.children
        this.cateLevel2.forEach(item => {
          this.cateLevel3 = item.children
        })

        this.scroll = this.scroll === 0 ? 1 : 0
      },
      //跳转到商品列表
      gotoGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      gotoSearch(){
        console.log("自定义中组件方法被触发");
        uni.navigateTo({
          url:"/subpkg/search/search"
        })
      }
    }
  }
</script>

<style lang="scss">
  .cate-box {
    display: flex;

    .scroll-left {
      width: 120px;

      .left-scroll-item {
        background-color: #F7F7F7;
        line-height: 60px;
        text-align: center;
        font-size: 12px;

        &.active {
          background-color: #fff;
          position: relative;

          &::before {
            content: " ";
            display: block;
            background-color: red;
            width: 3px;
            height: 30px;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }
    }
  }

  .scroll-right {
    background-color: #fff;

    .level2-box {
      .level2-title {
        text-align: center;
        padding: 10rpx 0;
        font-weight: bold;
        font-size: 26rpx;
      }


      .level3-box {
        display: flex;
        flex-wrap: wrap;
        text-align: center;
        margin: 0 10rpx;

        .level3-item {
          width: 33.33%;
          height: 250rpx;
          // display: flex;
          // flex-direction: column;
          // justify-content: center;

          .levle3-img {
            display: flex;
            width: 160rpx;
            height: 180rpx;

          }

          .level3-title {
            font-size: 20rpx;
          }
        }


      }

    }
  }
</style>